<?php
include '../include/function.php';
function valid_admin() {
    session_start();
    if (!isset($_SESSION['admin'])) {
        header("Location: login");
        return false;
    }
    return true;
}

function get_judge_status() {
    $database = connect_database();
    $query = "select judge_id, state from judges";
    $res = mysql_query($query, $database) or die(mysql_error());
    $cnt = 0;
    while ($row = mysql_fetch_row($res)) {
        $ret[$cnt]['judge_id'] = $row[0];
        $ret[$cnt]['state'] = $row[1];
        $cnt++;
    }
    return $ret;
}

function stop_judge($judge_id) {
    $database = connect_database();
    $query = "update judges set state='stop' where judge_id='$judge_id'";
    $res = mysql_query($query, $database) or die(mysql_error());
    return true;
}

function free_judge($judge_id) {
    $database = connect_database();
    $query = "update judges set state='free' where judge_id='$judge_id'";
    $res = mysql_query($query, $database) or die(mysql_error());
    return true;    
}

function get_problem_title($url) {
    $pattern = "/.+<title>\\d+?\\s+?-\\s+?(.+?)<\/title>.+/is";
    $replace = '\1';
    $str = file_get_contents($url);
    return preg_replace($pattern, $replace, $str);
}

function add_contest($title, $start_time, $end_time, $problem) {
    $database = connect_database();
    $query = "insert into contests (title, start_time, end_time) values ('$title', '$start_time', '$end_time')";
    $res = mysql_query($query, $database) or die(mysql_error());
    $query = "select max(contest_id) from contests";
    $res = mysql_query($query, $database) or die(mysql_error());
    $row = mysql_fetch_row($res);
    $contest_id = $row[0];

    $ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for ($i = 0; $i < count($problem); $i++) {
        $problem[$i]['title'] = addslashes($problem[$i]['title']);
        $problem[$i]['url'] = addslashes($problem[$i]['url']);
        $query = "insert into problems (title, url, uva_problem_id) 
                  values('{$problem[$i]['title']}','{$problem[$i]['url']}','{$problem[$i]['uva_problem_id']}')";
        $res = mysql_query($query, $database) or die(mysql_error());
        $query = "select max(problem_id) from problems";
        $res = mysql_query($query, $database) or die(mysql_error());
        $row = mysql_fetch_row($res);
        $problem_id = $row[0];
        $query = "insert into problems_in_contests (problem_id, contest_id, as_problem)
                  values('$problem_id', '$contest_id', '{$ch[$i]}')";
        $res = mysql_query($query, $database) or die(mysql_error());
    }
    return true;
}

function edit_contest($contest_id, $title, $start_time, $end_time) {
    $database = connect_database();
    $query = "update contests set title='$title', start_time='$start_time', end_time='$end_time'
              where contest_id=$contest_id";
    $res = mysql_query($query, $database) or die(mysql_error());
    return true;
}

function delete_contest($contest_id) {
    $database = connect_database();
    $query = "delete from contests where contest_id='$contest_id'";
    $res = mysql_query($query, $database) or die(mysql_error());

    $query = "delete from problems where problem_id in (select problem_id from problems_in_contests where contest_id='$contest_id')";
    $res = mysql_query($query, $database) or die(mysql_error());
    
    $query = "delete from problems_in_contests where contest_id='$contest_id'";
    $res = mysql_query($query, $database) or die(mysql_error());
    return true;
}

function delete_user($tmp_user_ids) {
    foreach ($tmp_user_ids as $tmp_user_id) {
        $database = connect_database();
        $query = "delete from users where user_id='$tmp_user_id'";
        $res = mysql_query($query, $database) or die(mysql_error());
    }
    return true;
}

function delete_tmp_user($tmp_user_ids) {
    foreach ($tmp_user_ids as $tmp_user_id) {
        $database = connect_database();
        $query = "delete from tmp_users where user_id='$tmp_user_id'";
        $res = mysql_query($query, $database) or die(mysql_error());
    }
    return true;
}

function validate_user($tmp_user_ids) {
    foreach ($tmp_user_ids as $tmp_user_id) {
        $database = connect_database();
        $query = "select user_id from users where user_id='$tmp_user_id'";
        $res = mysql_query($query, $database) or die(mysql_error());
        $row = mysql_fetch_row($res);
        if (!empty($row))
            continue;
        
        $query = "insert into users (user_id, nickname, password, email, school) select user_id, nickname, password, email, school from tmp_users where user_id='$tmp_user_id'";
        $res = mysql_query($query, $database) or die(mysql_error());
        $query = "delete from tmp_users where user_id='$tmp_user_id'";
        $res = mysql_query($query, $database) or die(mysql_error());
    }
    return true;
}

?>
